package sicnu.cs.ich.ich.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.stereotype.Service;
import sicnu.cs.ich.api.domain.entity.Ich;
import sicnu.cs.ich.api.domain.mq.IchMQ;
import sicnu.cs.ich.ich.service.IIchRankService;

/**
 * @author CaiKe
 * @date 2021/12/1 17:25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class IchRankServiceImpl implements IIchRankService {
    private final DefaultMQProducer producer;

    @Override
    public void ichTypeHitIncrementMQ(Integer typeId) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        var msg = new Message("ich","type",JSON.toJSONBytes(typeId));
        //发送
        producer.send(msg);
//        log.info("输出生产者：{}",msg);
    }

    @Override
    public void ichHitIncrement(Integer id) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        var msg = new Message("ich","ich",JSON.toJSONBytes(id));
        producer.send(msg);
    }

    @Override
    public void ichProvinceIncrement(Ich ich) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        final IchMQ ichMQ = IchMQ.builder()
                .province(ich.getProvince())
                .ichId(ich.getId())
                .name(ich.getName())
                .build();
        var msg = new Message("ich","province", JSON.toJSONBytes(ichMQ));
        producer.send(msg);
    }

    @Override
    public void updateIchHit(Integer ichId) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        var msg = new Message("ich","support", JSON.toJSONBytes(ichId));
        producer.send(msg);
    }
}